/**
 * 
 */
package com.yidu.drug.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.AuditModelMapper;
import com.yidu.drug.dao.ContractTableModelMapper;
import com.yidu.drug.dao.FinancialRecordModelMapper;
import com.yidu.drug.dao.FinancialSheetsModelMapper;
import com.yidu.drug.dao.PoBreakdownMapper;
import com.yidu.drug.dao.PurchaseOrderModelMapper;
import com.yidu.drug.dao.QtSumModelMapper;
import com.yidu.drug.dao.QualityTestingModelMapper;
import com.yidu.drug.model.AuditModel;
import com.yidu.drug.model.ContractTableModel;
import com.yidu.drug.model.FinancialRecordModel;
import com.yidu.drug.model.FinancialSheetsModel;
import com.yidu.drug.model.PoBreakdown;
import com.yidu.drug.model.PurchaseOrderModel;
import com.yidu.drug.model.QtSumModel;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.service.PurchaseOrderService;

/**
 * 总店采购订单表service实现类
 * @author 王志伟
 * @date 2018年8月9日 下午8:08:22
 * @version 1.0
 */
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService{
	
	//总店采购订单Dao
	@Resource
	private PurchaseOrderModelMapper purchaseOrderDao;

	//合同DAO
	@Resource
	private ContractTableModelMapper ctDao;
	
	//采购明细DAO
	@Resource
	private PoBreakdownMapper poDao;
	
	//质检总报表DAO
	@Resource
	private QtSumModelMapper qtSumDao;
	
	//质检DAO
	@Resource
	private QualityTestingModelMapper qtDao;
	
	//财务记录DAO
	@Resource
	private FinancialRecordModelMapper frDao;
	
	//财务DAO
	@Resource
	private FinancialSheetsModelMapper fsDao;
	
	//审核记录DAO
	@Resource
	private AuditModelMapper audDao;
	/**
	 * 根据采购订单ID删除
	 */
	@Override
	public int deleteByPrimaryKey(String poId) {
		// TODO Auto-generated method stub
		poDao.deleteByPoId(poId);
		return purchaseOrderDao.deleteByPrimaryKey(poId);
	}

	/**
	 * 增加采购订单所有信息
	 */
	@Override
	public int insert(PurchaseOrderModel record) {
		// TODO Auto-generated method stub
		return purchaseOrderDao.insert(record);
	}

	/**
	 * 增加采购订单部分信息
	 */
	@Override
	public int insertSelective(PurchaseOrderModel record) {
		// TODO Auto-generated method stub
		return purchaseOrderDao.insertSelective(record);
	}

	/**
	 * 根据采购订单ID查询所有
	 */
	@Override
	public PurchaseOrderModel selectByPrimaryKey(String poId) {
		// TODO Auto-generated method stub
		return purchaseOrderDao.selectByPrimaryKey(poId);
	}

	/**
	 * 根据采购订单ID修改部分信息
	 */
	@Override
	public int updateByPrimaryKeySelective(PurchaseOrderModel record) {
		// TODO Auto-generated method stub
		return purchaseOrderDao.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据采购订单ID修改所有信息
	 */
	@Override
	public int updateByPrimaryKey(PurchaseOrderModel record) {
		// TODO Auto-generated method stub
		return purchaseOrderDao.updateByPrimaryKey(record);
	}

	@Override
	public Map<String, Object> findAll(String limit, String page, String conId, String poState) {
		//NEW一个材料的模型,加入页面信息和类型名称
		PurchaseOrderModel purchaseOrderModel = new PurchaseOrderModel();
		Page pa=new Page();
		pa.setPage(Integer.valueOf(page));
		pa.setRows(Integer.valueOf(limit));
		purchaseOrderModel.setPage(pa);
		purchaseOrderModel.setConId(conId);
		purchaseOrderModel.setPoState(poState);
		//查询所有信息或者按条件查询
		List<PurchaseOrderModel> list=purchaseOrderDao.findAll(purchaseOrderModel);
		for (int j=0; j<list.size(); j++) {
	        /*
	         * 查询合同供应商名称
	         */
	        String contId=list.get(j).getConId();
	        if(!Tools.isEmpty(contId)){
	        	ContractTableModel ct=ctDao.selectByPrimaryKey(contId);
	  	        System.out.println("材料类型名称"+ct.getTwoCompany());
	  	        list.get(j).setGys(ct.getTwoCompany());
	        }
	        String zt[]=list.get(j).getPoState().split(",");
	        list.get(j).setShzt(zt[0]);//审核状态
	        list.get(j).setCgzt(zt[1]);//采购状态
	        list.get(j).setTjzt(zt[2]);//提交状态
	        list.get(j).setZjzt(zt[3]);//质检状态
	        list.get(j).setRkzt(zt[4]);//入库状态
		}
		//查询出的信息的总行数
		Long count = purchaseOrderDao.findRows(purchaseOrderModel);
		System.out.println("总行数"+count);
		//加入MAP集合
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("code",0);
		map.put("msg","");
		map.put("count",count);
		map.put("data", list);
		System.out.println("==="+new Gson().toJson(list));
		return map;
	}

	@Override
	public int updateTjzt(String poId, String poSumnum) {
		int row=0;
		
		QtSumModel qtSum=new QtSumModel();
		String qtSumId=Tools.getRandomUUID();
		qtSum.setSumId(qtSumId);
		qtSum.setSumSumnum(Integer.valueOf(poSumnum));
		qtSum.setPoId(poId);
		qtSum.setIsva(Tools.ISVA_YES);
		qtSum.setFilId("1");
		qtSum.setSumNopass(0);
		qtSum.setSumRate(new BigDecimal(0));
		qtSum.setSumRemark("未质检");
		row=qtSumDao.insertSelective(qtSum);
		if(row>0){
			List<PoBreakdown> poList=poDao.findMatIdByPoId(poId);
			for (PoBreakdown poBreakdown : poList) {
				QualityTestingModel qt=new QualityTestingModel();
				qt.setQtId(Tools.getRandomUUID());
				qt.setSumId(qtSumId);
				qt.setQtSumnum(poBreakdown.getPobNum());
				qt.setPwId(poBreakdown.getMatId());
				qt.setIsva(Tools.ISVA_YES);
				qt.setNopassNum(0);
				qt.setQtRate(new BigDecimal(0));
				//qt.setQtRemark("未质检");
				qtDao.insertSelective(qt);
			}
			PurchaseOrderModel po=new PurchaseOrderModel();
			po.setPoId(poId);
			po.setPoState("已审核,已采购,已提交,未质检,未入库");
			row=purchaseOrderDao.updateByPrimaryKeySelective(po);
		}
		return row;
	}

	@Override
	public int updateCgzt(String poId, String poSumprice) {
		int row=0;
		//财务
		String fsId=fsDao.findByIds("1");
		FinancialSheetsModel financialSheets = fsDao.selectByFilId("1");
		if(financialSheets != null){
			Double fsSumPrice=fsDao.findSumMoney("1");
			//财务记录
			FinancialRecordModel fr=new FinancialRecordModel();
			fr.setFrId(Tools.getRandomUUID());
			fr.setFsId(fsId);
			fr.setPwId(poId);
			fr.setFrName("支出");
			fr.setFrSumprice(new BigDecimal(fsSumPrice));
			fr.setFrPrice(new BigDecimal(poSumprice));
			fr.setFrRemark("采购");
			fr.setIsva(Tools.ISVA_YES);
			row=frDao.insertSelective(fr);
			if(row>0){
				Double poSumprices=Double.valueOf(poSumprice);
				Double price=fsSumPrice-poSumprices;
				System.out.println("总金额：+"+fsSumPrice+" 支出金额：+"+poSumprices+" 减出金额：+"+price);
				FinancialSheetsModel fs=new FinancialSheetsModel();
				fs.setFsId(fsId);
				fs.setFsPrice(new BigDecimal(price));
				int count=fsDao.updateByPrimaryKeySelective(fs);
				if(count>0){
					PurchaseOrderModel po=new PurchaseOrderModel();
					po.setPoId(poId);
					po.setPoState("已审核,已采购,未提交,未质检,未入库");
					row=purchaseOrderDao.updateByPrimaryKeySelective(po);
				}
			}
		}
		return row;
	}

	/**
	 * 修改财务审核
	 */
	@Override
	public int updateShzt(AuditModel aud) {
		aud.setAuditId(Tools.getRandomUUID());
		aud.setIsva(Tools.ISVA_YES);
		int count=audDao.insertSelective(aud);
		if(aud.getAuditPass().equals("通过")){
			if(count>0){
				System.out.println("审核记录已添加==========================");
				PurchaseOrderModel po=new PurchaseOrderModel();
				po.setPoId(aud.getQwId());
				po.setPoState("已审核,未采购,未提交,未质检,未入库");
				purchaseOrderDao.updateByPrimaryKeySelective(po);
			}else{
				System.out.println("审核记录未添加==========================");
			}
		}
		return count;
	}
	
}
